package atri.controller;

import atri.common.Result;
import atri.mapper.BookMapper;
import atri.model.Book;
import atri.model.User;
import atri.service.BookService;
import atri.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController

public class BookController {


    @Autowired
    public BookService service;
    @Autowired
    public BookMapper mapper;
    @Autowired
    public UserService userService;

    @PostMapping("/book")
    public Result save (@RequestBody Book book){
        int a =mapper.insert(book);
        if(a!=0)
        {return Result.success(a);}
        else
        {return Result.fail();}
    }

    @DeleteMapping("/book/{id}")
    public Result remove(@PathVariable int id){
        Boolean a = service.removeById(id);
        if(a)
        {return Result.success(a);}
        else
        {return Result.fail();}
    }

    @PutMapping("/book")
    public Result updateById(@RequestBody Book book){
        Boolean a = service.updateById(book);
        if(a)
        {return Result.success(a);}
        else
        {return Result.fail();}
    }

    @GetMapping("/book")
    public Result Getlist(){
        List<Book> s = new ArrayList<>();
        s=service.list();
        if(s!=null)
        {return  Result.success(s);}
        else
        {return Result.fail();}
    }

    @GetMapping("/book/{id}")
    public Result GetByid(@PathVariable int id){
        Book book = new Book();
        book = service.get_a_book(id);
        if(book != null)
        {return Result.success(book);}
        else
        {return Result.fail();}
    }

    @GetMapping("/book_n/{id}")
    public Result GetByid_Username(@PathVariable int id){
        User user = new User();
        user = userService.getById(id);
        if(user != null)
        {return Result.success(user);}
        else
        {return Result.fail();}
    }

    @GetMapping("/book_f/{id}")
    public Result GetByf_id(@PathVariable int id){
        List<Book> books = new ArrayList<>();
        books = service.getByf_id(id);
        for (Book book : books) {
            System.out.println(book);
        }
        if(books != null)
        {return Result.success(books);}
        else
        {return Result.fail();}
    }

    @GetMapping("/book_u/{id}")
    public Result GetBy_User_id(@PathVariable int id){
        List<Book> books = new ArrayList<>();
        books = service.get_by_user(id);
        for (Book book : books) {
            System.out.println(book);
        }
        if(books != null)
        {return Result.success(books);}
        else
        {return Result.fail();}
    }

    @GetMapping("/book/s01/{content}")
    public Result search_by_content(@PathVariable("content") String content){
        List<Book> commesnt = new ArrayList<>();
        commesnt = service.get_by_content(content);
        if(commesnt!=null)
        {return Result.success(commesnt);}
        else
        {return Result.fail();}
    }
    @GetMapping("/book/s02/{tags}")
    public Result search_by_tags(@PathVariable("tags") String content){
        List<Book> commesnt = new ArrayList<>();
        commesnt = service.find_by_category(content);
        if(commesnt!=null)
        {return Result.success(commesnt);}
        else
        {return Result.fail();}
    }
    @GetMapping("/book_add/{id}")
    public Result add_view(@PathVariable int id){
        Boolean a = false;
        a = service.add_view(id);
        if(a != null)
        {return Result.success(a);}
        else
        {return Result.fail();}
    }

    @PostMapping("/book_stars")
    public Result add_stars(@RequestBody Map<String, Object> request){
        Boolean a = false;
        System.out.println(request);
        Integer bookId = (Integer) request.get("bookId");
        Float rating = ((Number) request.get("rating")).floatValue();
        a = service.stars_new(rating,bookId);
        if(a != null)
        {return Result.success(a);}
        else
        {return Result.fail();}
    }

}
